package org.ninjarope.cinderstained.main.gameplay.definition.towerscript;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;

public class TowerScriptTokenizer {
	private String[] tokens;
	private int marker = 0;
	private int length = 0;
	public TowerScriptTokenizer(String filename) throws IOException {
		final FileHandle f = Gdx.files.internal(filename);
		final BufferedReader r = new BufferedReader(new InputStreamReader(
				f.read()));
		String line = "";
		String full = "";
		while ((line = r.readLine()) != null) {
			full += line.trim();
		}

		StringTokenizer t = new StringTokenizer(full, " \n{}();:\"\'%", true);
		this.length = t.countTokens();
		tokens = new String[this.length];
		
		int i = 0;
		while (t.hasMoreTokens()) {
			tokens[i++] = t.nextToken();			
		}
	}
	
	public String get() {		
		return tokens[marker];
	}
	
	public String next() {
		return tokens[marker++];
	}
	
	public String prev() {
		return tokens[--marker];
	}
	
	public boolean more() {
		return marker < length - 1;
	}
	
	public boolean expect(String e) throws IOException {
		return next().equalsIgnoreCase(e);
	}

	public void need(String e) throws IOException {
		if(!next().equalsIgnoreCase(e)) {
			throw new IOException(e + " needed, " + get() + " found.");
		}
	}	
}
